<?php

namespace app\admin\model\tongcheng;

use addons\tongcheng\model\BaseModel;


class RefundLogModel extends BaseModel
{
    // 表名
    protected $name = 'tongcheng_refund_log';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'refund_channel_text',
        'status_text',
        'refundable_url'
    ];

    public function getRefundableUrlAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['refundable_type']) ? $data['refundable_type'] : '');
        $list  = [
            DeliveryOrderModel::class                         => 'tongcheng/delivery_order/index',
            \addons\tongcheng\model\DeliveryOrderModel::class => 'tongcheng/delivery_order/index',
            "app\\admin\\model\\tongcheng\\delivery\\Order"   => 'tongcheng/delivery_order/index',
            // 'app\admin\model\tongcheng\order\WithdrawModel'   => 'tongcheng/rider_withdraw/index',
        ];
        return isset($list[$value]) ? url($list[$value], ['id' => $data['refundable_id']]) : '';
    }


    public function getRefundChannelList()
    {
        return ['WECHAT' => __('WECHAT'), 'BALANCE' => __('BALANCE')];
    }

    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2'), '3' => __('Status 3')];
    }


    public function getRefundChannelTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['refund_channel']) ? $data['refund_channel'] : '');
        $list  = $this->getRefundChannelList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list  = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function user()
    {
        return $this->belongsTo('app\admin\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function zone()
    {
        return $this->belongsTo('Zone', 'zone_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    /**
     * 多态关联变动对象
     * @return \think\model\relation\MorphTo
     */
    public function refundable()
    {
        return $this->morphTo();
    }
}
